<?php
/**
* alltosun.com  cheers活动页面
* ============================================================================
* 版权所有 (C) 2009-2015 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 张元昊 (zhangyh@alltosun.com) $
* $Date: 2015-07-22 11:09:12 $
* $Id$
*/

class Action
{

    public function lucky_draw()
    {
        $user_id = user_helper::get_user_id();

        $data = '<div id="pop-wzj" class="popup"><a href="javascript:clearDrawHtml()" class="close"><img src="'.STATIC_URL.'/images/724img/close.png" alt="关闭"></a><div class="bann"><img src="'.STATIC_URL.'/images/724img/pc-pop-banner-mqd.png" ></div><div class="txt">抱歉，活动已结束，谢谢您的关注与参与！</div><div class="btn"><a href="javascript:clearDrawHtml()">关闭</a></div></div>';
        return array('info' => 'ok', 'html' => $data);

        if(!$user_id) {
            $data = '<div id="pop-login" class="popup none1"><a href="javascript:clearDrawHtml()" class="close"><img src="'.STATIC_URL.'/images/724img/close.png" alt="关闭"></a><div class="bann"><img src="'.STATIC_URL.'/images/724img/pc-pop-banner-mqd.png" ></div><div class="txt danger">您还没有登陆NBD<br>现在马上登录，千万不要错过秒杀啊！</div><div class="btn"><a href="'.AnUrl('user/login').'">登录</a></div><div class="ps"><a href="'.AnUrl('user/register').'">马上注册</a></div></div>';
            return array('info' => 'ok', 'html' => $data);
        }

        $prize_type = trim(Request::Post('type', 0));
        $prize_list = events_config::$cheers_prize_type;

        if(!$prize_list[$prize_type]) {
            return array('info' => 'error', 'msg' => '参数错误，请重新尝试！');
        }
        $user_config_list = events_config::$cheers_lucky_draw;

        if($prize_type == '1') {
            $other_html = '<a href="http://www.thinkworldshop.com.cn/activity/stack/index.html" target="_blank">了解更多关于ThinkPad Stack&gt;&gt;</a>';
        } else {
            $other_html = '<a href="http://www.newbd.com/goods/19.html" target="_blank">了解更多关于贝比猫&gt;&gt;</a>';
        }

        $draw_info = _uri('cheers_lucky_draw', array('user_id' => $user_id, 'type' => $prize_type));
        if($draw_info) {
            if($draw_info['is_prize']) {
                if($draw_info['realname'] && $draw_info['mobile']) {
                    $data = '<div id="pop-zj" class="popup"><div class="bann"><img src="'.STATIC_URL.'/images/724img/pc-pop-banner-zj.jpg" alt="恭喜您！中奖啦！"></div><dl class="bar"><dt>您中的是</dt><dd>'.$prize_list[$draw_info['type']].'</dd></dl><div class="form"><h4>您已经留下您的姓名和电话号码了<br>我们的工作人员会联系您发奖</h4></div></div><script>$(function(){setTimeout(clearDrawHtml,3000)})</script>';
                    return array('info' => 'ok', 'html' => $data);
                }
                $data = '<div id="pop-zj" class="popup"><div class="bann"><img src="'.STATIC_URL.'/images/724img/pc-pop-banner-zj.jpg" alt="恭喜您！中奖啦！"></div><dl class="bar"><dt>您中的是</dt><dd>'.$prize_list[$draw_info['type']].'</dd></dl><div class="form"><h4 class="js_errorInfo">请留下您的姓名及手机号码<br>我们的工作人员会联系您发奖</h4><form action="'.AnUrl('events/cheers/update_address').'" method="post" class="js_addressFrom"><input type="hidden" name="type" value="'.$draw_info['type'].'"><div class="control"><input type="text" name="realname" class="js_realname" placeholder="请输入您的姓名"></div><div class="control"><input type="text" name="mobile" class="js_mobile" placeholder="请输入您的手机号"></div></form><div class="btn js_fromSubmit"><a href="javascript:void(0);">提交</a></div></div></div>';
                return array('info' => 'ok', 'html' => $data);
            } else {
                $data = '<div id="pop-wzj" class="popup"><a href="javascript:clearDrawHtml()" class="close"><img src="'.STATIC_URL.'/images/724img/close.png" alt="关闭"></a><div class="bann"><img src="'.STATIC_URL.'/images/724img/pc-pop-banner-mqd.png" ></div><div class="txt">抱歉，每个用户一个奖品只有一次抽取机会！</div><div class="btn"><a href="javascript:clearDrawHtml()">参与更多724活动</a></div><div class="ps">'.$other_html.'</div></div>';
                return array('info' => 'ok', 'html' => $data);
            }
        } else {
            $info = array(
                'user_id' => $user_id,
                'type'    => $prize_type
            );
            $cheers_id = _model('cheers_lucky_draw')->create($info);
        }

        if(!in_array($user_id, $user_config_list[$prize_type])) {
            $data = '<div id="pop-wzj" class="popup"><a href="javascript:clearDrawHtml()" class="close"><img src="'.STATIC_URL.'/images/724img/close.png" alt="关闭"></a><div class="bann"><img src="'.STATIC_URL.'/images/724img/pc-pop-banner-mqd.png" ></div><div class="txt">很遗憾，您未抽中该礼品</div><div class="btn"><a href="javascript:clearDrawHtml()">参与更多724活动</a></div><div class="ps">'.$other_html.'</div></div>';
            return array('info' => 'ok', 'html' => $data);
        } else {
            $data = '<div id="pop-zj" class="popup"><div class="bann"><img src="'.STATIC_URL.'/images/724img/pc-pop-banner-zj.jpg" alt="恭喜您！中奖啦！"></div><dl class="bar"><dt>您中的是</dt><dd>'.$prize_list[$prize_type].'</dd></dl><div class="form"><h4 class="js_errorInfo">请留下您的姓名及手机号码<br>我们的工作人员会联系您发奖</h4><form action="'.AnUrl('events/cheers/update_address').'" method="post" class="js_addressFrom"><input type="hidden" name="type" value="'.$prize_type.'"><div class="control"><input type="text" name="realname" class="js_realname" placeholder="请输入您的姓名"></div><div class="control"><input type="text" name="mobile" class="js_mobile" placeholder="请输入您的手机号"></div></form><div class="btn js_fromSubmit"><a href="javascript:void(0);">提交</a></div></div></div>';
            _model('cheers_lucky_draw')->update($cheers_id, array('is_prize' => 1));
            return array('info' => 'ok', 'html' => $data);
        }

    }

    public function update_address()
    {
        $user_id = user_helper::get_user_id();
        if(!$user_id) {
            return array('info' => 'error', 'msg' => '请登录后进行抽奖');
        }

        $realname = AnFilter::filter_string(Request::Post('realname', ''));
        $mobile   = AnFilter::filter_string(Request::Post('mobile', ''));
        $type     = Request::Post('type', 0);

        if(!$type) {
            return array('参数错误，请重新尝试！', 'error', AnUrl(''));
        }

        if(empty($realname) || empty($mobile)) {
            return array('姓名和手机号不可以为空', 'error', AnUrl(''));
        }

        if(!user_helper::is_mobile($mobile)) {
            return array('手机号格式不正确', 'error', AnUrl(''));
        }

        $filter = array(
                'user_id' => $user_id,
                'type'    => $type
            );
        $cheers_info = _uri('cheers_lucky_draw', $filter);
        if(!$cheers_info || $cheers_info['is_prize'] != 1) {
            return array('您没有中奖，请联系客服！', 'error', AnUrl(''));
        }

        $affected_id = _model('cheers_lucky_draw')->update($cheers_info['id'], array('realname' => $realname, 'mobile' => $mobile));
        if($affected_id) {
            return array('资料提交成功，客服会联系您！', 'ok', AnUrl(''));
        }
    }

    public function purchase()
    {
        $user_id = user_helper::get_user_id();

        if(!$user_id) {
            $data = '<div id="pop-login" class="popup none1"><a href="javascript:clearDrawHtml()" class="close"><img src="'.STATIC_URL.'/images/724img/close.png" alt="关闭"></a><div class="bann"><img src="'.STATIC_URL.'/images/724img/pc-pop-banner-mqd.png" ></div><div class="txt danger">您还没有登陆NBD<br>现在马上登录，千万不要错过秒杀啊！</div><div class="btn"><a href="'.AnUrl('user/login').'">登录</a></div><div class="ps"><a href="'.AnUrl('user/register').'">马上注册</a></div></div>';
            return array('info' => 'ok', 'html' => $data);
        }

        $goods_type = trim(Request::Post('type', 0));
        $goods_list = events_config::$cheers_purchase_list;

        if(!$goods_list[$goods_type]) {
            return array('info' => 'error', 'msg' => '参数错误，请重新尝试！');
        }

        $period_time = (int)date('H', time());
        if($period_time%2 != 0) {
            $period_time = $period_time-1;
        }
        $period_time = 24;
        if($period_time == 24) {
            $notice_next = '';
        } else {
            if($goods_type != 16) {
                $next_time = $period_time+2;
                $next_num  = $goods_list[$goods_type][1]/12;
            } else {
                $next_time = 24;
                $next_num  = 1;
            }
            $notice_next = '<div class="ps"><a href="javascript:void(0);" target="_blank">'.$next_time.'点再来，还有'.$next_num.'个'.$goods_list[$goods_type][0].'产品等你抢！</a></div>';
        }
        $nopurchase_html ='<div id="pop-wqd" class="popup"><a href="javascript:clearDrawHtml()" class="close"><img src="'.STATIC_URL.'/images/724img/close.png" alt="关闭"></a><div class="bann"><img src="'.STATIC_URL.'/images/724img/pc-pop-banner-mqd.png"></div><div class="txt">活动已经结束，谢谢您的关注与参与！</div><div class="btn"><a href="javascript:clearDrawHtml()">关闭</a></div></div>';
        $purchase_html ='<div id="pop-qdl" class="popup"><div class="bann"><img src="'.STATIC_URL.'/images/724img/pc-pop-banner-qd.jpg" alt="恭喜您！抢到啦！"></div><div class="btn"><a href="'.AnUrl('goods/sku?id='.$goods_type).'">去购买</a></div></div>';

        // $user_goods_list = events_config::$cheers_goods_list;
        $filter = array('user_id'=>$user_id, 'goods_id'=>$goods_type);

        $purchase_log_info = _model('cheers_purchase_log')->read($filter);

        $purchase_info = _uri('cheers_purchase', array('user_id' => $user_id));

        if($purchase_info) {  //是否抢购中过
            if($purchase_info['goods_id'] == $goods_type) {  //抢购商品ID是否等于本次抢购ID
                return array('info' => 'ok', 'html' => $purchase_html);
            } else {
                return array('info' => 'ok', 'html' => $nopurchase_html);
            }
        }
        return array('info' => 'ok', 'html' => $nopurchase_html);
        //记录log
        _model('cheers_purchase_log')->create($filter);

        // if($purchase_log_info) {
        //     return array('info' => 'ok', 'html' => $nopurchase_html);
        // }

        $rand_num = rand(10,10000);
        $cheers_purchase_info = _model('cheers_purchase')->read(array('user_id' => 0, 'purchase_time ' => $period_time,'goods_id' => $goods_type, $rand_num=>$rand_num));
        if($cheers_purchase_info){
            $affected = _model('cheers_purchase')->update($cheers_purchase_info['id'], array('user_id' => $user_id));
            if($affected) {
                return array('info' => 'ok', 'html' => $purchase_html);
            }
        }
        return array('info' => 'ok', 'html' => $nopurchase_html);
    }



    public function addPurchase()
    {
        exit;
        $goods_list = events_config::$cheers_purchase_list;

        foreach($goods_list as $k => $v) {
            $start_time = 2;
            for($i = 0;$i < 12; $i++) {
                if($k == 16) {
                    if($i == 5 || $i == 11) {
                        $filter = array(
                            'goods_id' => $k,
                            'purchase_time' => $start_time
                        );
                        _model('cheers_purchase')->create($filter);
                    }
                } else {
                    for($ii = 0;$ii < ($v[1]/12); $ii++) {
                        $filter = array(
                            'goods_id' => $k,
                            'purchase_time' => $start_time
                        );
                        _model('cheers_purchase')->create($filter);
                    }
                }
                $start_time += 2;
            }


            
        }



    }

}